// pages/questionaire/sharequestionnaire/createposter/createposter.js
import{getQuestionqrCode} from "../../../../api/questionnaire"
import {API_HOST} from "../../../../config"
import {getStorageItem} from "../../../../utils/storage"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageiconPath:"",
    headerImgPath:"",
    user_name:"",
    qrcodeUrl:"",
    bannerUrl:"https://ustatic.hudongmiao.com/joymewAssistant/createposterbanner.png",
    avatorUrl:"https://ustatic.hudongmiao.com/joymewAssistant/newAdd/temporaryicon.png",
    imagePath:"",
    splid:"",
    questitle:"",
    introduce:"",

  },

  savePoster: function () {
 
    let that = this;
 
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success(res) {
        wx.showModal({
          content: "图片已保存到相册，赶紧晒一下吧~",
          showCancel: false,
          confirmText: "好的",
          confirmColor: "#333",
          success: function (res) {
 
             
            // wx.hideLoading();
          },
          fail: function (res) {},
        });
      },
      fail(err) {
 
        if (
          err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
          err.errMsg === "saveImageToPhotosAlbum:fail auth deny" ||
          err.errMsg === "saveImageToPhotosAlbum:fail authorize no response"
        ) {
          wx.hideLoading();
          wx.showModal({
            title: "提示",
            content: "需要您授权保存相册",
            showCancel: false,
            success: (modalSuccess) => {
              wx.openSetting({
                success(settingdata) {
        
                },
                fail(failData) {
     
                },
                complete(finishData) {
   
                },
              });
            },
          });
        }
      },
    });
  },
  downLoadImg: function (netUrl, storageKeyUrl) {
    return new Promise((resolve, reject) => {
      wx.getImageInfo({
        src: netUrl, //请求的网络图片路径
        success: function (res) {
          //请求成功后将会生成一个本地路径即res.path,然后将该路径缓存到storageKeyUrl关键字中
          wx.setStorageSync(storageKeyUrl, res.path);
          resolve(res.path);
        },
        fail: function (err) {
          reject(err);
        },
      });
    });
  },
  
  save2phone: function() {
    // this.radiusicon()
    let ctx = wx.createCanvasContext("savePoster");
    let qr = wx.getStorageSync('qrcodeUrl');
    let usericon = wx.getStorageSync('usericonpath')
 
    let that = this;
    ctx.setFillStyle('white')
    ctx.fillRect(0,0,500,400)
    ctx.fillStyle="black"
    ctx.setFontSize(15)
    ctx.fillText(this.data.user_name,200,336)
    ctx.setFontSize(12)
    ctx.fillStyle="#666666"
    ctx.fillText('邀请你来填写',150,377)
    
    ctx.drawImage(this.data.bannerUrl,20,20,335,250)
    ctx.drawImage(usericon,150,310,40,40)
    ctx.drawImage(qr,70,310,70,70)
    ctx.setFontSize(18)
    ctx.fillStyle="white"
    ctx.fillText('婚礼问卷调查',80,230)
    ctx.stroke()
    ctx.draw()
    ctx.save()



 
    setTimeout(function() {
      wx.canvasToTempFilePath({
        canvasId: "savePoster",
        x:0,
        y:0,
        width: 375,
        height: 500,
        success: function(res) {
           
          that.setData({
            imagePath:res.tempFilePath
          })
          that.savePoster()
        }
      })
    },1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_name = getStorageItem('user_name')
    let headerImgPath = getStorageItem("headerImgPath")
 
    // this.widget = this.selectComponent('.widget')
 
    this.setData({
      splid:options.splid,
      questitle:options.questitle,
      introduce:options.introduce,
      user_name:user_name,
      headerImgPath:headerImgPath
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
    this.downLoadImg(this.data.headerImgPath,"usericonpath")
 
    let that = this
    
    getQuestionqrCode({
      "url": `${API_HOST}hm-course/#/questionnaire?splid=${that.data.splid}`,
      // "url": `${API_HOST}/hm-course/#/questionnaire?splid=${that.data.splid}&questitle=${that.data.questitle}&introduce=${that.data.introduce}`,
      // "url": `${API_HOST}hm-course/#/questionnaire?splid=${options.splid}&questitle=${encodeURI(options.questitle)}&introduce=${encodeURI(options.introduce)}`,
      "type": "question"
    }).then((res)=>{
       
      this.setData({
        qrcodeUrl:res.data.end_url
      })
      this.downLoadImg(res.data.end_url,"qrcodeUrl")
      
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})